package com.victor.controller.app;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.city.CityCenterLocation;
import com.victor.entity.jingqu.Jingqulist;
import com.victor.entity.jingqu.JingquticketIntroduce;
import com.victor.service.JingquService;
import com.victor.utils.LocationUtils;
import com.victor.utils.Msg;
/**
 * 景区
 * @author 
 *
 */
@RestController
@RequestMapping("/jingqu")
public class JingquAppController {
	@Autowired
	private JingquService jingquService;
	
	/**
	 * 景区详情
	 * @param jqid
	 * @param uid
	 * @param page
	 * @param row
	 * @param flag
	 * @return
	 */
	@RequestMapping("/appdetail")
	public Msg xiangqing(String jqid,String uid,int page,int row,String flag) {
		if (jqid == null || jqid.isEmpty() || flag == null || flag.isEmpty()) {
			return Msg.fail("入参失败");
		}
		if (uid == null || uid.isEmpty()) {
			Jingqulist jingqulist = jingquService.selByJqid(jqid);
			return Msg.success().add("jingqu", jingqulist);
		}else {
			Jingqulist selByJqidAndUid = jingquService.selByJqidAndUid(jqid,uid);
			return Msg.success().add("jingqu", selByJqidAndUid);
		}
	}
	/**
	 * 景区列表
	 * @param city
	 * @param classid
	 * @param level
	 * @param location(距离)
	 * @param screening(1-低价优先  2-高价优先)
	 * @param page
	 * @param row
	 * @param mylon
	 * @param mylat
	 * @return
	 */
	@RequestMapping("/jingqulist")
	public Msg jingquFirstPage(String search,String city,String classid,String level,String area,String location,String screening,int page,int row,String mylon,String mylat) {
		if (city ==  null || city.isEmpty()) {
			return Msg.fail("入参错误");
		}
		List<Jingqulist> jingqulists = new ArrayList<>();
		//距离
		if (location != null && location.isEmpty()) {
			if (mylon != null && !mylon.isEmpty() && mylat != null && !mylat.isEmpty()) {
				
				//查询城市所有景区
				List<Jingqulist> list = jingquService.selByCity(city);
				for (int i = 0; i < list.size(); i++) {
					String distance = LocationUtils.getDistance(mylon, mylat, list.get(i).getJqlocation_lon(), list.get(i).getJqlocation_lat());
					if (Integer.valueOf(distance)< Integer.valueOf(location)) {
						jingqulists.add(list.get(i));
					}
				}
				return Msg.success().add("list", jingqulists);
				
			}else {
				//查询城市所有景区
				List<Jingqulist> list = jingquService.selByCity(city);
				//查询市中心经纬度
				CityCenterLocation selCenterCityByName = jingquService.selCenterCityByName(city);
				for (int j = 0; j < list.size(); j++) {
					String distance = LocationUtils.getDistance(selCenterCityByName.getC_lon(), selCenterCityByName.getC_lat(), list.get(j).getJqlocation_lon(), list.get(j).getJqlocation_lat());
					if (Integer.valueOf(distance)< Integer.valueOf(location)) {
						jingqulists.add(list.get(j));
					}
				}
				return Msg.success().add("list", jingqulists);
			}
		}
		//筛选
		if (screening != null && !screening.isEmpty()) {
			List<Jingqulist> selShaixuan = jingquService.selShaixuan(screening,city);
			return Msg.success().add("list", selShaixuan);
		}
		PageInfo<Jingqulist> selMoreCondition = jingquService.selMoreCondition(search,city,classid,level,area,page,row,mylon,mylat);
		return Msg.success().add("list", selMoreCondition);
	}
		
	/**
	 * 查询 须知
	 * @param id  票id
	 * @return
	 */
	@RequestMapping("/findintroduce")
	public Msg jingquTicketIntrosuce(String id) {
		if (id == null || id.isEmpty()) {
			return Msg.fail("入参失败");
		}
		JingquticketIntroduce selTicketIntroduceByTid = jingquService.selTicketIntroduceByTid(id);
		return Msg.success().add("info", selTicketIntroduceByTid);
		
	}
}
